package com.pan.admin.module.datareport.entity;

import com.gitee.apanlh.util.valid.Assert;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * 开始时间和结束时间
 *
 * @author caoxin
 */
@Data
public class StartTimeAndEndTime {

    @DateTimeFormat(iso = ISO.DATE)
    private LocalDate startTime;
    @DateTimeFormat(iso = ISO.DATE)
    private LocalDate endTime;

    /**
     * 如果存在null
     */
    public boolean ifNull() {
        return this.startTime == null || this.endTime == null;
    }

    /**
     * 如果存在null则生成近1天
     */
    public void ifNullGen1() {
        if (this.ifNull()) {
            LocalDate now = LocalDate.now();
            this.startTime = now.plusDays(0);
            this.endTime = now.plusDays(1);
        }
    }

    /**
     * 如果存在null则生成近7天
     */
    public void ifNullGen7() {
        if (this.ifNull()) {
            LocalDate now = LocalDate.now();
            this.startTime = now.plusDays(-6);
            this.endTime = now.plusDays(1);
        }
    }

    public List<LocalDate> dayList() {

        Assert.isNotNull(this.startTime);
        Assert.isNotNull(this.endTime);
        // 开始时间 应小于等于 结束时间
        Assert.isFalse(this.startTime.isAfter(this.endTime));

        long between = ChronoUnit.DAYS.between(this.startTime, this.endTime);
        Assert.isTrue(between >= 0L && between <= 10000L);

        List<LocalDate> result = new ArrayList<>((int) between);
        for (long i = 0; i < between; i++) {
            LocalDate temp = this.startTime.plusDays(i);
            result.add(temp);
        }

        return result;
    }

}
